package com.wechat.utils;

import com.auth.vo.wechat.TreeObject;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:Tangjiachang
 * @Date 2023/7/29
 * @Description: 菜单工具类
 */
public class MenuUtils {
    /**
     * 构建树形结构
     *
     * @author Tangjiachang
     * @date 2023/7/29 13:15
     * @param topTree:
     * @return T
     **/
    public static <T extends TreeObject> List<T> builderTreeMenu(List<T> topTree, int queryTop) {
        final List<T> trees = Lists.newArrayList();
        topTree.stream()
                .filter(item -> item.getParentId().intValue() == queryTop)
                .collect(Collectors.toList())
                .forEach((parentMenu) -> {
                    trees.add(builderChildren(parentMenu, topTree));
                });
        return trees;
    }

    /**
     * 构建子列表
     *
     * @author Tangjiachang
     * @date 2023/7/29 13:15
     * @param children:
	 * @param parants:
     * @return T
     **/
    public static <T extends TreeObject> T builderChildren(T children, List<T> parants) {
        // 遍历所有的菜单数据,判断id和parentId对应关系
        parants.stream().forEach(parant -> {
            if (parant.getParentId().intValue() == children.getId().intValue()) {
                children.getChildren().add(builderChildren(parant, parants));
            }
        });
        return children;
    }
}
